﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using O.M.Production.Application.IService;
using O.M.Production.Application.Service;
using O.M.Production.Domain.Entity;
using O.M.Production.ErrorCodeEnum;
using System.Linq.Expressions;

namespace O.M.Production.Read.API.Controllers
{
    /// <summary>
    /// Api控制器-工序管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Read_Working_Procedure_ManageController : ControllerBase
    {
        private readonly I_working_procedure_manage_Service _Working_Procedure_Manage_Service;

        public Read_Working_Procedure_ManageController(I_working_procedure_manage_Service working_Procedure_Manage_Service)
        {
            _Working_Procedure_Manage_Service = working_Procedure_Manage_Service;
        }

        /// <summary>
        /// 分页查询-工序管理
        /// </summary>
        /// <param name="WId_WName">工序id或工序名称</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<IActionResult> Get_working_procedure_manage(string? WId_WName, int pageSize, int pageIndex)
        {
            try
            {
                var list=await _Working_Procedure_Manage_Service.Get_working_procedure_manage(WId_WName, pageSize, pageIndex);
                return Ok(list);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 导出列表到Excel
        /// </summary>
        /// <returns>Excel文件</returns>
        [HttpGet]
        public async Task<IActionResult> Post_Derive_Excel()
        {
            try
            {
                var list = await _Working_Procedure_Manage_Service.GetShowInforAsync();
                var ms = new MemoryStream();
                ms.SaveAs(list);
                ms.Seek(0, SeekOrigin.Begin);
                return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    FileDownloadName = "工序管理.xlsx"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
